/* *****************************************************************************
 * JFab (http://code.google.com/p/jfab)
 * Copyright (c) 2011 JFab.org
 * Admin jfab@jeffreythompson.net
 *
 * See the file "LICENSE.txt" for information on usage and redistribution of
 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
 * *****************************************************************************/

package org.jfab.core.analyzer;

import java.util.Comparator;

import org.apache.commons.lang3.StringUtils;
import org.jfab.core.collection.CommentList;
import org.jfab.core.factory.CGAnnotationFactory;
import org.jfab.core.factory.CGTypeFactory;
import org.jfab.core.factory.StringFactory;
import org.jfab.core.model.CGObject;
import org.jfab.core.model.CGTypeUtilities;
import org.jfab.core.model.TypeFactory;

/**
 * Provides base functionality for a compilation unit analyzer.
 * 
 * @param <T> Return type.
 * @param <U> Source type.
 */
public abstract class AbstractCompilationUnitAnalyzer<T extends CGObject, U>
        extends AbstractAnalyzer<T, U>
{
    /**
     * Construct this object with the given parameters.
     * 
     * @param typeFactory Type factory.
     * @param cgTypeUtils Code generator type utilities. (required)
     * @param cgTypeFactory Code generator type factory.
     * @param stringFactory String factory.
     * @param annotationFactory Annotation factory.
     * @param comparator Comparator.
     */
    public AbstractCompilationUnitAnalyzer(final TypeFactory typeFactory,
            final CGTypeUtilities cgTypeUtils,
            final CGTypeFactory cgTypeFactory,
            final StringFactory stringFactory,
            final CGAnnotationFactory annotationFactory,
            final Comparator<T> comparator)
    {
        super(typeFactory, cgTypeUtils, cgTypeFactory, stringFactory,
                annotationFactory, comparator);
    }

    /**
     * @param line Line.
     * 
     * @return the comment lines (if any) from the given parameter.
     */
    @Override
    protected final CommentList parseCommentLines(final String line)
    {
        CommentList answer = null;

        if (line != null)
        {
            final String key00 = "package";
            int index00 = line.indexOf(key00);
            String text = line.substring(index00);

            final String key11 = ";";
            index00 = text.indexOf(key11);
            text = text.substring(index00);

            final String key0 = "/**";
            final int index0 = text.indexOf(key0);

            if (index0 >= 0)
            {
                text = text.substring(index0);

                final String key1 = "*/";
                final String comment = StringUtils.substringBetween(text, key0,
                        key1);

                if (comment != null)
                {
                    final String[] commentLines = comment.split("\n");
                    answer = new CommentList();

                    for (String commentLine : commentLines)
                    {
                        commentLine = cleanupCommentLine(commentLine);
                        answer.add(commentLine);
                    }
                }
            }
        }

        return answer;
    }
}
